package com.wang.project.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wang.project.annotation.AuthCheck;
import com.wang.project.common.BaseResponse;
import com.wang.project.common.ErrorCode;
import com.wang.project.common.ResultUtils;
import com.wang.project.constant.UserConstant;
import com.wang.project.exception.BusinessException;
import com.wang.project.model.dto.integral.IntegralOrderDeleteRequest;
import com.wang.project.model.dto.integral.IntegralOrderQueryRequest;
import com.wang.project.model.dto.integral.IntegralOrderUpdateRequest;
import com.wang.project.model.vo.integral.IntegralOrderVO;
import com.wang.project.service.IntegralOrderService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 积分订单
 */
@RestController
@RequestMapping("/integralOrder")
public class IntegralOrderController {

    @Resource
    private IntegralOrderService integralOrderService;

    /**
     * 获取订单列表
     *
     * @param integralOrderQueryRequest
     * @param request
     * @return
     */
    @GetMapping("/list")
    public BaseResponse<List<IntegralOrderVO>> listIntegralOrder(IntegralOrderQueryRequest integralOrderQueryRequest, HttpServletRequest request) {
        List<IntegralOrderVO> integralOrderVOS = integralOrderService.listIntegralOrder(integralOrderQueryRequest, request);
        return ResultUtils.success(integralOrderVOS);
    }

    /**
     * 分页查询
     *
     * @param integralOrderQueryRequest
     * @param request
     * @return
     */
    @GetMapping("/page")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<IntegralOrderVO>> pageIntegralOrder(IntegralOrderQueryRequest integralOrderQueryRequest, HttpServletRequest request) {
        Page<IntegralOrderVO> integralOrderVOPage = integralOrderService.pageIntegralOrder(integralOrderQueryRequest, request);
        return ResultUtils.success(integralOrderVOPage);
    }

    /**
     * 删除订单
     *
     * @param integralOrderDeleteRequest
     * @return
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> removeIntegralOrder(@RequestBody IntegralOrderDeleteRequest integralOrderDeleteRequest) {
        if (integralOrderDeleteRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(integralOrderService.removeById(integralOrderDeleteRequest.getId()));
    }

    /**
     * 更新订单信息
     *
     * @param integralOrderUpdateRequest
     * @return
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateIntegralOrder(@RequestBody IntegralOrderUpdateRequest integralOrderUpdateRequest) {
        Boolean result = integralOrderService.updateIntegralOrder(integralOrderUpdateRequest);
        return ResultUtils.success(result);
    }


}
